<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;
use think\response\Redirect;

class Index extends BaseController
{
    public function index()
    {
        return View::fetch('login');
    }

    public function loginFun()
    {
        $data = Request::param();
        $res = Db::table('user')->where('username', $data['username'])->find();
        if ($res == null) {
            return json(0);
        } else {
            $md5password = md5($data['password']);
            if ($md5password == $res['password']) {
                Session::set('user', $data['username']);
                return json(1);
            } else {
                return json(2);
            }
        }

    }

    public function mian()
    {
        $sion = Session::get('user');
        if ($sion == null) {
            return redirect('index/index');
        } else {
            $data = Db::table('lst')->select();
            $dataNum = count($data);
            $aarData = $data->toArray();
            $arrLst = array_column($aarData, 'num');
            $arrSum = array_sum(array_column($aarData, 'num'));
            $arrLstCount = count($arrLst);
            $priceLst = array_column($aarData, 'price');
            for ($i = 0; $i < $arrLstCount; $i++) {
                $priceSum[$i] = $arrLst[$i] * $priceLst[$i];
            }
            $priceSum = array_sum($priceSum);
            View::assign('arrSum', $arrSum);
            View::assign('priceSum', $priceSum);
            View::assign('dataNum', $dataNum);
            View::assign('list', $data);
            View::assign('user', $sion);
            return View::fetch('mian');
        }
    }

    public function isLogin()
    {
        $sion = Session::get('user');
        if ($sion == null) {
            $resSion = 0;
        } else {
            $resSion = 1;
        }
        return $resSion;
    }

    public function sub()
    {
        if ($this->isLogin() == 0) {
            return Redirect('index/index');
        } else {
            return View::fetch('sub');
        }
    }

    public function out()
    {
        if ($this->isLogin() == 0) {
            return Redirect('index/index');
        } else {
            $data = Db::table('lst')->select();


            View::assign('list', $data);
            return View::fetch('out');


//            $data = Db::table('lst')->column('name');
//            View::assign('name', $data);
//            return View::fetch('out');
        }
    }

    public function outFun()
    {
        $data = Request::param();
//        print_r($data['select']);
        // table方法必须指定完整的数据表名
        $res = Db::table('lst')->where('name', $data['select'])->find();
//        print_r($res['num']);
        if ($res !== null) {
            $num = $data['num'] - $res['num'];
            if ($res['num'] == 0) {
                echo '库存为0，请补货或修改库存后再试！';
            } else if ($num > 0) {
                echo '库存仅为' . $res['num'] . ',请补货或修改库存';
            } else {
                $res1 = Db::table('lst')
                    ->where('name', $data['select'])
                    ->dec('num', $data['num'])
                    ->update();
                if ($res1 !== 0) {
                    $sion = Session::get('user');
                    $reportData = [
                        'user' => $sion,
                        'name' => $res['name'],
                        'num' => $data['num']
                    ];
                    $resReport = Db::name('report')->insert($reportData);
                    if ($resReport !== 0) {
                        echo '销售记录完成';
                    } else {
                        echo '销售记录完成,但未加入到销售统计中，请自行记录';
                    }
                } else {
                    echo '销售记录失败，请重试';
                }
            }
        }
    }


    public function back()
    {
        if ($this->isLogin() == 0) {
            return Redirect('index/index');
        } else {
            $data = Db::table('lst')->column('name');
            View::assign('name', $data);
            return View::fetch('back');
        }
    }

    public function backFun()
    {
        $data = Request::param();
//        print_r($data['select']);
        $res = Db::table('lst')->where('name', $data['select'])->find();
//        print_r($res['num']);
        if ($res !== null) {
            $res1 = Db::table('lst')
                ->where('name', $data['select'])
                ->inc('num', $data['num'])
                ->update();
            if ($res1 !== 0) {
                $sion = Session::get('user');
                $reportData = [
                    'user' => $sion,
                    'name' => $res['name'],
                    'num' => -$data['num']
                ];
                $resReport = Db::name('report')->insert($reportData);
                if ($resReport !== 0) {
                    echo '销售退回完成,并统计销售数据';
                } else {
                    echo '销售退回完成,但未加入到销售统计中，请自行记录';
                }
            } else {
                echo '销售退回失败，请重试';
            }
        }
    }


    public function change()
    {
        if ($this->isLogin() == 0) {
            return Redirect('index/index');
        } else {
            $data = Request::param();
            $res = Db::table('lst')
                ->where('name', $data['name'])
                ->find();
            View::assign('list', $res);
            return View::fetch('change');
        }
    }

    public function changeFun()
    {
        $data = Request::param();
        $res = Db::name('lst')
            ->where('name', $data['name'])
            ->data($data)
            ->update();
        if ($res !== 0) {
            return json(1);
        } else {
            return json(0);
        }
    }

    public function del()
    {
        $data = Request::param();
        $res = Db::table('lst')->where('name', $data['name'])->delete();
        if ($res == 0) {
            return json(0);
        } else {
            return json(1);
        }
    }

    public function reg()
    {
        $data = Request::param();
        $res = Db::table('lst')->where('name', $data['name'])->find();
        if ($res == null) {
            $res = Db::name('lst')->insert($data);
            if ($res !== 1) {
                return json(0);
            } else {
                return json(1);
            }
        } else {
            return json(-1);
        }
    }

    public function search()
    {
        if ($this->isLogin() == 0) {
            return Redirect('index/index');
        } else {
            $data = Request::param();
//        print_r($data);
            $dataser = $data['keys'];
            $data = Db::table('lst')
                ->where('name', 'like', "%$dataser%")
                ->whereOr('firm', 'like', "%$dataser%")
                ->whereOr('sort', 'like', "%$dataser%")
                ->select();
            $dataNum = count($data);
            $aarData = $data->toArray();
            if (count($data) !== 0) {
                $arrLst = array_column($aarData, 'num');
                $arrSum = array_sum(array_column($aarData, 'num'));
                $arrLstCount = count($arrLst);
                $priceLst = array_column($aarData, 'price');
                for ($i = 0; $i < $arrLstCount; $i++) {
                    $priceSum[$i] = $arrLst[$i] * $priceLst[$i];
                }
                $priceSum = array_sum($priceSum);
                View::assign('arrSum', $arrSum);
                View::assign('priceSum', $priceSum);
                View::assign('dataNum', $dataNum);
                View::assign('list', $data);
                View::assign('dataser', $dataser);
            } else {
                View::assign('arrSum', 0);
                View::assign('priceSum', 0);
                View::assign('dataNum', 0);
                View::assign('list', $data);
                View::assign('dataser', $dataser);
            }
            return View::fetch('search');
        }

    }

    public function warning()
    {
        if ($this->isLogin() == 0) {
            return Redirect('index/index');
        } else {
            $data = Db::table('lst')
                ->where('num', '<=', '3')
                ->select();
            $dataNum = count($data);
            $aarData = $data->toArray();
            $arrSum = array_sum(array_column($aarData, 'num'));
            View::assign('dataNum', $dataNum);
            View::assign('list', $data);
            return View::fetch('index/warning');
        }
    }

    public function report()
    {
        $sion = Session::get('user');
        if ($sion == null) {
            return redirect('index/index');
        } else {
            $data = Db::table('report')->where('user', $sion)->select()->toArray();
            $data = array_reverse($data, false);
            $dataCount = count($data);
            View::assign('dataCount', $dataCount);
            View::assign('list', $data);
            View::assign('user', $sion);
            return View::fetch('report');
        }
    }

    public function forms()
    {
        return View::fetch('index/forms');
    }


    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
}
